// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   TemplateNode.java

package com.oa.document;

import com.oa.database.*;
import com.oa.util.IndoString;
import java.io.PrintStream;
import java.sql.*;
import java.util.StringTokenizer;

public class TemplateNode
{

    private int template_id;
    private int before_id;
    private int node_id;
    private DataConn dataConn;
    private Connection conn;
    private int template_errcode;
    private String template_errstr;

    public TemplateNode(DataConn dataConnection)
    {
        template_id = 0;
        before_id = 0;
        node_id = 0;
        dataConn = null;
        conn = null;
        template_errcode = 0;
        template_errstr = "";
        dataConn = dataConnection;
    }

    public void setTemplateId(int id)
    {
        template_id = id;
    }

    public void setBeforeId(int id)
    {
        before_id = id;
    }

    private void setErrorMessage()
    {
        template_errcode = dataConn.getErrorCode();
        template_errstr = dataConn.getErrorMessage();
    }

    public int getErrorCode()
    {
        return template_errcode;
    }

    public String getErrorMessage()
    {
        return template_errstr;
    }

    public void setNodeId(int id)
    {
        node_id = id;
    }

    public String getNodeString()
    {
        ResultSet rs = null;
        String sTable = "";
        String sField = "";
        String sFieldCondition = "";
        String sValueCondition = "";
        String nodeString = "";
        QueryData queryData = new QueryData(dataConn);
        sTable = "OA_LCMB";
        sField = "LCJD";
        sFieldCondition = "LCBJ,LCID";
        sValueCondition = "0," + template_id;
        queryData.setRecordSet(sTable, sField, sFieldCondition, sValueCondition);
        rs = queryData.getResultSet();
        try
        {
            if(rs.next())
                nodeString = rs.getString("LCJD");
        }
        catch(SQLException sqlexception) { }
        if(nodeString == null)
            nodeString = "";
        return nodeString;
    }

    public String replaceNode(String strNode)
    {
        String str_before = Integer.toString(before_id);
        String str_node = Integer.toString(node_id);
        String nodeString = "";
        if(str_before.equals("0") || str_before.equals(""))
            nodeString = str_node + ";";
        for(StringTokenizer sTokenizer = new StringTokenizer(strNode, ";", false); sTokenizer.hasMoreElements();)
        {
            String temp = sTokenizer.nextElement().toString();
            if(!temp.equals("0") && !temp.equals(""))
                nodeString = nodeString + temp + ";";
            if(temp.equals(str_before))
                nodeString = nodeString + str_node + ";";
        }

        if(nodeString.length() > 0)
            nodeString = nodeString.substring(0, nodeString.length() - 1);
        else
            nodeString = str_node;
        return nodeString;
    }

    public void sortNode(String strNode)
    {
        String sTable = "";
        String sField = "";
        String sValue = "";
        String sFieldCondition = "";
        String sValueCondition = "";
        UpdateData updateData = new UpdateData(dataConn);
        sTable = "OA_LCJD";
        sField = "JDSX";
        sFieldCondition = "JDID";
        String nodeString = "";
        StringTokenizer sTokenizer = new StringTokenizer(strNode, ";", false);
        int i = 1;
        while(sTokenizer.hasMoreElements()) 
        {
            sValueCondition = sTokenizer.nextElement().toString();
            if(!sValueCondition.equals("") && !sValueCondition.equals("0"))
            {
                sValue = Integer.toString(i);
                updateData.update(sTable, sField, sValue, sFieldCondition, sValueCondition);
                i++;
            }
        }
    }

    public void updateNode(String strNode)
    {
        String sTable = "";
        String sField = "";
        String sValue = "";
        String sFieldCondition = "";
        String sValueCondition = "";
        UpdateData updateData = new UpdateData(dataConn);
        sTable = "OA_LCMB";
        sField = "LCJD";
        sValue = "'" + strNode + "'";
        sFieldCondition = "LCID";
        sValueCondition = Integer.toString(template_id);
        updateData.update(sTable, sField, sValue, sFieldCondition, sValueCondition);
        System.out.println(updateData.getErrorMessage());
    }

    public void insertNode(int iTemplateId, int iBeforeId, int iNodeId)
    {
        setTemplateId(iTemplateId);
        setBeforeId(iBeforeId);
        setNodeId(iNodeId);
        String nodeString = getNodeString();
        nodeString = replaceNode(nodeString);
        updateNode(nodeString);
        sortNode(nodeString);
    }

    public String getTemplate(int user_id)
    {
        String sTemplateId = "0";
        String sUser = Integer.toString(user_id);
        if(sUser.equals("0") || sUser.equals(""))
            return sTemplateId;
        String sTable = "OA_LCMB AS A, OA_LCJD AS B";
        String sField = "A.LCID,B.JDRY";
        String sCond = " A.LCID = B.JDLC AND A.LCBJ=0 AND B.JDBJ=0 AND B.JDSX =1";
        String sFieldSort = "A.LCID";
        QueryData queryData = new QueryData(dataConn);
        queryData.setMultRecordSet(sTable, sField, sCond, sFieldSort, 0);
        ResultSet rs = queryData.getResultSet();
        IndoString indoString = new IndoString();
        try
        {
            while(rs.next()) 
            {
                int template_id = rs.getInt("LCID");
                String template_person = rs.getString("JDRY");
                if(indoString.indexOf(template_person, ";", sUser) > 0)
                    sTemplateId = sTemplateId + "," + template_id;
            }
        }
        catch(Exception exception) { }
        return sTemplateId;
    }

    public int deleteNode(int template, int node)
    {
        setTemplateId(template);
        setNodeId(node);
        try
        {
            DeleteData deleteData = new DeleteData(dataConn);
            deleteData.delete("OA_LCJD", "JDID", Integer.toString(node));
        }
        catch(Exception e)
        {
            template_errstr = e.getMessage().toString();
        }
        String nodeString = getNodeString();
        IndoString indoString = new IndoString();
        nodeString = indoString.remove(nodeString, ";", Integer.toString(node));
        updateNode(nodeString);
        sortNode(nodeString);
        return 0;
    }

    public static void main(String args[])
    {
        DataConn dataConn = new DataConn();
        TemplateNode templateNode = new TemplateNode(dataConn);
        System.out.println(templateNode.getTemplate(5));
    }
}
